#pragma once

#include <fstream>
#include <string>
#include <sstream>
#include <iostream>

#ifdef WIN32
#include "windows.h"
#endif

#ifndef WIN32
#define MACOS
#endif

#ifdef MACOS
  #include <Carbon/Carbon.h> 
  #include <CoreFoundation/CoreFoundation.h>
#endif

namespace Wonderland{
  namespace Utility{

    inline std::string slurp(std::string filename) {
      std::ifstream input(filename.c_str());
      std::stringstream sstr;
      sstr << input.rdbuf();
      return sstr.str();
    }

    inline void fatalException(std::string message){
      #ifdef WIN32
      MessageBoxA(
        NULL,
        message.c_str(),
        "Wonderland could not start",
        MB_ICONERROR
      );
	#endif

	#ifdef MACOS    
	std::cout << "[ERROR] Wonderland could not start\n\n" << message << std::endl;
	#endif

	exit(-1);
    }
  }

  namespace Locations{
	#ifdef MACOS
	inline std::string MacBundlePath(){
	    char path[1024];
	    CFBundleRef mainBundle = CFBundleGetMainBundle();
	    assert(mainBundle);
	    CFURLRef mainBundleURL = CFBundleCopyBundleURL(mainBundle);
	    assert(mainBundleURL);
	    CFStringRef cfStringRef = CFURLCopyFileSystemPath( mainBundleURL, kCFURLPOSIXPathStyle);
	    assert(cfStringRef);
	    CFStringGetCString(cfStringRef, path, 1024, kCFStringEncodingASCII);
	    CFRelease(mainBundleURL);
	    CFRelease(cfStringRef);
	    return std::string(path);
	}
	#endif

    inline std::string Cache(){
      return "cache/";
    }

    inline std::string Resources(){
		#ifdef WIN32
		return "resources/";
		#else
		return MacBundlePath() + "/Contents/resources/";
		#endif
    }
  }
}
